﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

namespace WebApplication
{
    public partial class MenuControl : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            InitializeMenu();
        }

        private void InitializeMenu()
        {
            var currentPage = PageFromUrl(Request.Url.ToString());

            //Animate the main menu items
            AnimateMenu(currentPage, "~/pages/Page1.aspx", SearchHyperLink, SearchMenuItem);
            AnimateMenu(currentPage, "~/pages/Page2.aspx", TodoHyperLink, TodoMenuItem);
            AnimateMenu(currentPage, "~/pages/Page3.aspx", AboutHyperLink, AboutMenuItem);

            SearchSubMenu.Hide();
            AnimateMenu(currentPage, "~/pages/Page1.aspx", SearchCommitteeHyperLink, SearchCommitteeSubMenuItem, SearchSubMenu, SearchMenuItem);
            AnimateMenu(currentPage, "~/pages/Page4.aspx", SearchBuildingHyperLink, SearchBuildingSubMenuItem, SearchSubMenu, SearchMenuItem);
            AnimateMenu(currentPage, "~/pages/Page5.aspx", SearchDepartmentHyperLink, SearchDepartmentSubMenuItem, SearchSubMenu, SearchMenuItem);
            AnimateMenu(currentPage, "~/pages/Page6.aspx", SearchPersonHyperLink, SearchPersonSubMenuItem, SearchSubMenu, SearchMenuItem);

            TodoSubMenu.Hide();
            AnimateMenu(currentPage, "~/pages/Page2.aspx", TodoAdministrationHyperLink, TodoAdministrationSubMenuItem, TodoSubMenu, TodoMenuItem);
            AnimateMenu(currentPage, "~/pages/Page7.aspx", ToDoTrainingHyperLink, TodoTrainingSubMenuItem, TodoSubMenu, TodoMenuItem);


        }


        private static bool AnimateMenu(string currentPage, string url, HyperLink link, HtmlControl menuItem)
        {
            link.NavigateUrl = url;

            var isSelected = IsPage(currentPage, url);
            menuItem.Attributes["class"] = isSelected ? "selected" : "tab";
            return isSelected;
        }

        private static void AnimateMenu(string currentPage, string url, HyperLink link, HtmlControl menuItem, HtmlGenericControl subMenu, HtmlGenericControl subMenuItem)
        {
            if (!AnimateMenu(currentPage, url, link, menuItem)) return;

            subMenu.Show();
            subMenuItem.Attributes["class"] = "selected";
        }

        private static bool IsPage(string currentPage, string url)
        {
            return currentPage.Equals(PageFromUrl(url), StringComparison.OrdinalIgnoreCase);
        }

        /// <summary>
        /// Gets the page name from the URL using regular expression.
        /// </summary>
        /// <param name="url">The URL.</param>
        /// <returns>The page name</returns>
        private static string PageFromUrl(string url)
        {
            const string pattern = @"/(?<page>[\w]+).aspx";
            var regex = new Regex(pattern, RegexOptions.IgnoreCase);

            var match = regex.Match(url);
            return match.Success ? match.Groups["page"].Value : string.Empty;
        }
    }
}